package com.airlines.crewassignment.service.ruleverification.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.airlines.crewassignment.constant.CrewConstant;
import com.airlines.crewassignment.constant.RuleConstant;
import com.airlines.crewassignment.domain.Crew;
import com.airlines.crewassignment.domain.CrewFlightExperience;
import com.airlines.crewassignment.domain.CrewLinkLineRoster;
import com.airlines.crewassignment.domain.Position;
import com.airlines.crewassignment.domain.rule.AssignmentRule;
import com.airlines.crewassignment.domain.util.SkillUtil;
import com.airlines.crewassignment.domain.util.SoftRuleUtil;
import com.airlines.crewassignment.entity.ruleverification.RuleVerification;
import com.airlines.crewassignment.service.optaplanner.OptaplannerService;
import com.airlines.crewassignment.service.rule.AssignmentRuleService;
import com.airlines.crewassignment.service.ruleverification.RuleHardCheckService;
import com.airlines.crewassignment.service.ruleverification.RuleSoftCheckService;

/**
 * @author xxxx
 * @date 2018年7月25日 
 * @description
 */
@Service
public class RuleSoftCheckServiceImpl implements RuleSoftCheckService{
	@Autowired
	private  OptaplannerService optaplannerService;
	@Autowired
	private RuleHardCheckService ruleHardCheckService;
	@Autowired
	private AssignmentRuleService assignmentRuleService;
	
	@Override
	public List<RuleVerification> ruleSoftCheckF(DateTime flightDateStart,DateTime flightDateEnd, CrewLinkLineRoster crewLinkLineRoster) {
		
		if(crewLinkLineRoster == null){
			crewLinkLineRoster = optaplannerService.crewLinkLineRosterInit(CrewConstant.DUTY_F,flightDateStart, flightDateEnd);
		}
		
		
		List<Position> positionList = crewLinkLineRoster.getPositionList();
		List<Crew> crewList = crewLinkLineRoster.getCrewList();
		
		for(Position p : positionList){
			for(Crew c : crewList){
				if(StringUtils.equals(p.getWorkcode(), c.getWorkcode())){
					p.setCrew(c);
				}
			}
		}
		List<RuleVerification> ruleHardCheckALl = new ArrayList<>();
		ruleHardCheckALl.addAll(this.F_S0001(crewLinkLineRoster));
		ruleHardCheckALl.addAll(this.F_S0002(crewLinkLineRoster));
		ruleHardCheckALl.addAll(this.F_S0003(crewLinkLineRoster));
		ruleHardCheckALl.addAll(this.F_S0004(crewLinkLineRoster));
		ruleHardCheckALl.addAll(this.F_S0005(crewLinkLineRoster));
		ruleHardCheckALl.addAll(this.F_S0006(crewLinkLineRoster));
		ruleHardCheckALl.addAll(this.F_S0007(crewLinkLineRoster));
		ruleHardCheckALl.addAll(this.F_S0008(crewLinkLineRoster));
		ruleHardCheckALl.addAll(this.F_S0009(crewLinkLineRoster));
		ruleHardCheckALl.addAll(this.F_S0010(crewLinkLineRoster));
		ruleHardCheckALl.addAll(this.F_S0011(crewLinkLineRoster));
		ruleHardCheckALl.addAll(this.F_S0012(crewLinkLineRoster));
		ruleHardCheckALl.addAll(this.F_S0014(crewLinkLineRoster));
		ruleHardCheckALl.addAll(this.F_S0015(crewLinkLineRoster));
		ruleHardCheckALl.addAll(this.F_S0017(crewLinkLineRoster));
		ruleHardCheckALl.addAll(this.F_S0018(crewLinkLineRoster));
		ruleHardCheckALl.addAll(this.F_S0019(crewLinkLineRoster));
		ruleHardCheckALl.addAll(this.F_S0020(crewLinkLineRoster));
		return ruleHardCheckALl;
	}
	
	//F_S0001	年飞行时间较少优先安排
	private List<RuleVerification> F_S0001(CrewLinkLineRoster crewLinkLineRoster) {
		List<RuleVerification>  ruleVerificationList = new ArrayList<>();
		List<Position> positionList = crewLinkLineRoster.getPositionList();
		String ruleCode = Thread.currentThread().getStackTrace()[1].getMethodName();
		AssignmentRule assignmentRule = ruleHardCheckService.getAssignmentRule(crewLinkLineRoster, ruleCode);
		if(!assignmentRuleService.assignmentRuleAbled(assignmentRule)){
			return ruleVerificationList;
		}
		for(Position p : positionList){
			if(StringUtils.isBlank(p.getWorkcode())){
				continue;
			}
			CrewFlightExperience crewFlightExperience = p.getCrew().getCrewFlightExperience();
			int rank = crewFlightExperience.getFlightDurationLastYearRank();
		
			
			int ruleScore = (100-rank)*assignmentRule.getRuleScore()/100;
			String remark = "近一年飞行时间："+crewFlightExperience.getFlightDurationLastYear()/60 +"小时"
					+",转换百分制名次为："+crewFlightExperience.getFlightDurationLastYearRank();
			
			RuleVerification r = ruleHardCheckService.getRuleVerification(p, true, remark, ruleCode, ruleScore);
			ruleVerificationList.add(r);
		}
		return ruleVerificationList;
	}
	
	//F_S0002	月飞行时间较少优先安排
	private List<RuleVerification> F_S0002(CrewLinkLineRoster crewLinkLineRoster) {
		List<RuleVerification>  ruleVerificationList = new ArrayList<>();
		List<Position> positionList = crewLinkLineRoster.getPositionList();
		String ruleCode = Thread.currentThread().getStackTrace()[1].getMethodName();
		AssignmentRule assignmentRule = ruleHardCheckService.getAssignmentRule(crewLinkLineRoster, ruleCode);
		if(!assignmentRuleService.assignmentRuleAbled(assignmentRule)){
			return ruleVerificationList;
		}
		for(Position p : positionList){
			if(StringUtils.isBlank(p.getWorkcode())){
				continue;
			}
			CrewFlightExperience crewFlightExperience = p.getCrew().getCrewFlightExperience();
			int rank = crewFlightExperience.getFlightDurationLastMonthRank();
			int ruleScore =  (100-rank)*assignmentRule.getRuleScore()/100;
			String remark = "近一月飞行时间："+crewFlightExperience.getFlightDurationLastMonth()/60 +"小时"
					+",转换百分制名次为："+rank;
			
			RuleVerification r = ruleHardCheckService.getRuleVerification(p, true, remark, ruleCode, ruleScore);
			ruleVerificationList.add(r);
		}
		return ruleVerificationList;
	}
	//F_S0003	距离上次结束休假或停飞时间越小，优先安排差航段
	private List<RuleVerification> F_S0003(CrewLinkLineRoster crewLinkLineRoster) {
		List<RuleVerification>  ruleVerificationList = new ArrayList<>();
		List<Position> positionList = crewLinkLineRoster.getPositionList();
		String ruleCode = Thread.currentThread().getStackTrace()[1].getMethodName();
		AssignmentRule assignmentRule = ruleHardCheckService.getAssignmentRule(crewLinkLineRoster, ruleCode);
		if(!assignmentRuleService.assignmentRuleAbled(assignmentRule)){
			return ruleVerificationList;
		}
		for(Position p : positionList){
			if(StringUtils.isBlank(p.getWorkcode())){
				continue;
			}
			if(p.getLinkLine().getBadRoute()== false){
				continue;
			}
			CrewFlightExperience crewFlightExperience = p.getCrew().getCrewFlightExperience();
			int rank = crewFlightExperience.getLastVacationAndStopFlyRank();
			int ruleScore =  (100-rank)*assignmentRule.getRuleScore()/100;
			String remark = "本航线为差航线"
					+",距离上一次休假或停飞天数："+crewFlightExperience.getLastVacationAndStopFly()
					+",转换百分制名次为："+rank;
			
			RuleVerification r = ruleHardCheckService.getRuleVerification(p, true, remark, ruleCode, ruleScore);
			ruleVerificationList.add(r);
		}
		return ruleVerificationList;
	}
	
	//F_S0004	如果本次航线为过夜航线，优先排最近一年内过夜次数较少的人员
	private List<RuleVerification> F_S0004(CrewLinkLineRoster crewLinkLineRoster) {
		List<RuleVerification>  ruleVerificationList = new ArrayList<>();
		List<Position> positionList = crewLinkLineRoster.getPositionList();
		String ruleCode = Thread.currentThread().getStackTrace()[1].getMethodName();
		AssignmentRule assignmentRule = ruleHardCheckService.getAssignmentRule(crewLinkLineRoster, ruleCode);
		if(!assignmentRuleService.assignmentRuleAbled(assignmentRule)){
			return ruleVerificationList;
		}
		for(Position p : positionList){
			if(StringUtils.isBlank(p.getWorkcode())){
				continue;
			}
			if(p.getLinkLine().getNightPass()== false){
				continue;
			}
			CrewFlightExperience crewFlightExperience = p.getCrew().getCrewFlightExperience();
			int rank = crewFlightExperience.getNightPassCountLastYearRank();	
			int ruleScore =  (100-rank)*assignmentRule.getRuleScore()/100;
			String remark  = "本航线为过夜航线"
					+"一年内过夜次："+crewFlightExperience.getNightPassCountLastYear()
					+",转换百分制名次为："+rank;
			RuleVerification r = ruleHardCheckService.getRuleVerification(p, true, remark, ruleCode, ruleScore);
			ruleVerificationList.add(r);
		}
		return ruleVerificationList;
	}
	
	//F_S0005	如果本次航线为过夜航线，优先排最近一月内过夜次数较少的人员
	private List<RuleVerification> F_S0005(CrewLinkLineRoster crewLinkLineRoster) {
		List<RuleVerification>  ruleVerificationList = new ArrayList<>();
		List<Position> positionList = crewLinkLineRoster.getPositionList();
		String ruleCode = Thread.currentThread().getStackTrace()[1].getMethodName();
		AssignmentRule assignmentRule = ruleHardCheckService.getAssignmentRule(crewLinkLineRoster, ruleCode);
		if(!assignmentRuleService.assignmentRuleAbled(assignmentRule)){
			return ruleVerificationList;
		}
		for(Position p : positionList){
			if(StringUtils.isBlank(p.getWorkcode())){
				continue;
			}
			if(p.getLinkLine().getNightPass()== false){
				continue;
			}
			CrewFlightExperience crewFlightExperience = p.getCrew().getCrewFlightExperience();
			int rank = crewFlightExperience.getNightPassCountLastMonthRank();	
			int ruleScore = (100-rank)*assignmentRule.getRuleScore()/100;
			String remark = "本航线为过夜航线"
					+"一月内过夜次："+crewFlightExperience.getNightPassCountLastMonth()
					+",转换百分制名次为："+rank;
			RuleVerification r = ruleHardCheckService.getRuleVerification(p, true, remark, ruleCode, ruleScore);
			ruleVerificationList.add(r);
		}
		return ruleVerificationList;
	}
	
	//F_S0006	如果本次航线为夜航航线，优先排最近一年内夜航次数较少的人员
	private List<RuleVerification> F_S0006(CrewLinkLineRoster crewLinkLineRoster) {
		List<RuleVerification>  ruleVerificationList = new ArrayList<>();
		List<Position> positionList = crewLinkLineRoster.getPositionList();
		String ruleCode = Thread.currentThread().getStackTrace()[1].getMethodName();
		AssignmentRule assignmentRule = ruleHardCheckService.getAssignmentRule(crewLinkLineRoster, ruleCode);
		if(!assignmentRuleService.assignmentRuleAbled(assignmentRule)){
			return ruleVerificationList;
		}
		for(Position p : positionList){
			if(StringUtils.isBlank(p.getWorkcode())){
				continue;
			}
			if(p.getLinkLine().getNightFly()== false){
				continue;
			}
			CrewFlightExperience crewFlightExperience = p.getCrew().getCrewFlightExperience();
			int rank = crewFlightExperience.getNightFlyCountLastYearRank();	
			int ruleScore = (100-rank)*assignmentRule.getRuleScore()/100;
			String remark = "本航线为夜航航线"
					+"一年内的夜航次数："+crewFlightExperience.getNightFlyCountLastYear()
					+"，转换百分制名次为："+rank;
			RuleVerification r = ruleHardCheckService.getRuleVerification(p, true, remark, ruleCode, ruleScore);
			ruleVerificationList.add(r);
		}
		return ruleVerificationList;
	}
	//F_S0007	如果本次航线为夜航航线，优先排最近一月内夜航次数较少的人员
	private List<RuleVerification> F_S0007(CrewLinkLineRoster crewLinkLineRoster) {
		List<RuleVerification>  ruleVerificationList = new ArrayList<>();
		List<Position> positionList = crewLinkLineRoster.getPositionList();
		String ruleCode = Thread.currentThread().getStackTrace()[1].getMethodName();
		AssignmentRule assignmentRule = ruleHardCheckService.getAssignmentRule(crewLinkLineRoster, ruleCode);
		if(!assignmentRuleService.assignmentRuleAbled(assignmentRule)){
			return ruleVerificationList;
		}
		for(Position p : positionList){
			if(StringUtils.isBlank(p.getWorkcode())){
				continue;
			}
			if(p.getLinkLine().getNightFly()== false){
				continue;
			}
			CrewFlightExperience crewFlightExperience = p.getCrew().getCrewFlightExperience();
			int rank = crewFlightExperience.getNightFlyCountLastMonthRank();	
			int ruleScore =  (100-rank)*assignmentRule.getRuleScore()/100;
			String remark = "本航线为夜航航线"
					+"一月内的夜航次数："+crewFlightExperience.getNightFlyCountLastMonth()
					+"，转换百分制名次为："+rank;
			RuleVerification r = ruleHardCheckService.getRuleVerification(p, true, remark, ruleCode, ruleScore);
			ruleVerificationList.add(r);
		}
		return ruleVerificationList;
	}
	
	//F_S0008	如果本次航线的航段数大于3段，优先排最近一年内飞航段数大于3段较少的人员
	private List<RuleVerification> F_S0008(CrewLinkLineRoster crewLinkLineRoster) {
		List<RuleVerification>  ruleVerificationList = new ArrayList<>();
		List<Position> positionList = crewLinkLineRoster.getPositionList();
		String ruleCode = Thread.currentThread().getStackTrace()[1].getMethodName();
		AssignmentRule assignmentRule = ruleHardCheckService.getAssignmentRule(crewLinkLineRoster, ruleCode);
		if(!assignmentRuleService.assignmentRuleAbled(assignmentRule)){
			return ruleVerificationList;
		}
		for(Position p : positionList){
			if(StringUtils.isBlank(p.getWorkcode())){
				continue;
			}
			if( p.getLinkLine().getFlightInfoList().size() <= RuleConstant.FLIGHT_MORE_THAN_COUNT){
				continue;
			}
			CrewFlightExperience crewFlightExperience = p.getCrew().getCrewFlightExperience();
			int rank = crewFlightExperience.getFlightMoreThanCountLastYearRank();	
			int ruleScore = (100-rank)*assignmentRule.getRuleScore()/100;
			String remark = "本航线航段数大于3"
					+"一年内航段大于3的次数："+crewFlightExperience.getFlightMoreThanCountLastYear()
					+"，转换百分制名次为："+rank;
			RuleVerification r = ruleHardCheckService.getRuleVerification(p, true, remark, ruleCode, ruleScore);
			ruleVerificationList.add(r);
		}
		return ruleVerificationList;
	}
	
	//F_S0009	如果本次航线的航段数大于3段，优先排最近一月内飞航段数大于3段较少的人员
	private List<RuleVerification> F_S0009(CrewLinkLineRoster crewLinkLineRoster) {
		List<RuleVerification>  ruleVerificationList = new ArrayList<>();
		List<Position> positionList = crewLinkLineRoster.getPositionList();
		String ruleCode = Thread.currentThread().getStackTrace()[1].getMethodName();
		AssignmentRule assignmentRule = ruleHardCheckService.getAssignmentRule(crewLinkLineRoster, ruleCode);
		if(!assignmentRuleService.assignmentRuleAbled(assignmentRule)){
			return ruleVerificationList;
		}
		for(Position p : positionList){
			if(StringUtils.isBlank(p.getWorkcode())){
				continue;
			}
			if( p.getLinkLine().getFlightInfoList().size() <= RuleConstant.FLIGHT_MORE_THAN_COUNT){
				continue;
			}
			CrewFlightExperience crewFlightExperience = p.getCrew().getCrewFlightExperience();
			int rank = crewFlightExperience.getFlightMoreThanCountLastMonthRank();	
			int ruleScore =  (100-rank)*assignmentRule.getRuleScore()/100;
			String remark = "本航线航段数大于3"
					+"一月内航段大于3的次数："+crewFlightExperience.getFlightMoreThanCountLastMonth()
					+"，转换百分制名次为："+rank;
			RuleVerification r = ruleHardCheckService.getRuleVerification(p, true, remark, ruleCode, ruleScore);
			ruleVerificationList.add(r);
		}
		return ruleVerificationList;
	}
	
	//F_S0010	如果本次航段被标记为差，优先安排最近一年飞标记为差航段较少的人员
	private List<RuleVerification> F_S0010(CrewLinkLineRoster crewLinkLineRoster) {
		List<RuleVerification>  ruleVerificationList = new ArrayList<>();
		List<Position> positionList = crewLinkLineRoster.getPositionList();
		String ruleCode = Thread.currentThread().getStackTrace()[1].getMethodName();
		AssignmentRule assignmentRule = ruleHardCheckService.getAssignmentRule(crewLinkLineRoster, ruleCode);
		if(!assignmentRuleService.assignmentRuleAbled(assignmentRule)){
			return ruleVerificationList;
		}
		for(Position p : positionList){
			if(StringUtils.isBlank(p.getWorkcode())){
				continue;
			}
			if(p.getLinkLine().getBadRoute()== false){
				continue;
			}
			CrewFlightExperience crewFlightExperience = p.getCrew().getCrewFlightExperience();
			int rank = crewFlightExperience.getBadFlightCountLastYearRank();	
			int ruleScore =  (100-rank)*assignmentRule.getRuleScore()/100;
			String remark = "本航线为差航线"
					+"一年内差航线次数："+crewFlightExperience.getBadFlightCountLastYear()
					+"，转换百分制名次为："+rank;
			RuleVerification r = ruleHardCheckService.getRuleVerification(p, true, remark, ruleCode, ruleScore);
			ruleVerificationList.add(r);
		}
		return ruleVerificationList;
	}
	
	//F_S0011	如果本次航段被标记为差，优先安排最近一月飞标记为差航段较少的人员
	private List<RuleVerification> F_S0011(CrewLinkLineRoster crewLinkLineRoster) {
		List<RuleVerification>  ruleVerificationList = new ArrayList<>();
		List<Position> positionList = crewLinkLineRoster.getPositionList();
		String ruleCode = Thread.currentThread().getStackTrace()[1].getMethodName();
		AssignmentRule assignmentRule = ruleHardCheckService.getAssignmentRule(crewLinkLineRoster, ruleCode);
		if(!assignmentRuleService.assignmentRuleAbled(assignmentRule)){
			return ruleVerificationList;
		}
		for(Position p : positionList){
			if(StringUtils.isBlank(p.getWorkcode())){
				continue;
			}
			if(p.getLinkLine().getBadRoute()== false){
				continue;
			}
			CrewFlightExperience crewFlightExperience = p.getCrew().getCrewFlightExperience();
			int rank = crewFlightExperience.getBadFlightCountLastMonthRank();	
			int ruleScore =  (100-rank)*assignmentRule.getRuleScore()/100;
			String remark = "本航线为差航线"
					+"一月内差航线次数："+crewFlightExperience.getBadFlightCountLastMonth()
					+"，转换百分制名次为："+rank;
			RuleVerification r = ruleHardCheckService.getRuleVerification(p, true, remark, ruleCode, ruleScore);
			ruleVerificationList.add(r);
		}
		return ruleVerificationList;
	}
	
	//F_S0012	优先安排6个月内未执行过的任务环--》如果本航段起飞-目的机场，该飞行员6月内没有执行过，则优先安排
	private List<RuleVerification> F_S0012(CrewLinkLineRoster crewLinkLineRoster) {
		List<RuleVerification>  ruleVerificationList = new ArrayList<>();
		List<Position> positionList = crewLinkLineRoster.getPositionList();
		String ruleCode = Thread.currentThread().getStackTrace()[1].getMethodName();
		AssignmentRule assignmentRule = ruleHardCheckService.getAssignmentRule(crewLinkLineRoster, ruleCode);
		if(!assignmentRuleService.assignmentRuleAbled(assignmentRule)){
			return ruleVerificationList;
		}
		for(Position p : positionList){
			if(StringUtils.isBlank(p.getWorkcode())){
				continue;
			}
			boolean hasAirportExperienceFlag = SoftRuleUtil.hasAirportExperience(p.getCrew() , p.getLinkLine());
				
			if(!hasAirportExperienceFlag){
				int ruleScore = assignmentRule.getRuleScore();
				String remark = "6个月未执行该航线";
				RuleVerification r = ruleHardCheckService.getRuleVerification(p, true, remark, ruleCode, ruleScore);
				ruleVerificationList.add(r);
			}
		
		}
		return ruleVerificationList;
	}
	////F_S0014	如果本次航线的左座为教员，优先安排经历时间较少的副驾。
	private List<RuleVerification> F_S0014(CrewLinkLineRoster crewLinkLineRoster) {
		List<RuleVerification>  ruleVerificationList = new ArrayList<>();
		List<Position> positionList = crewLinkLineRoster.getPositionList();
		String ruleCode = Thread.currentThread().getStackTrace()[1].getMethodName();
		AssignmentRule assignmentRule = ruleHardCheckService.getAssignmentRule(crewLinkLineRoster, ruleCode);
		if(!assignmentRuleService.assignmentRuleAbled(assignmentRule)){
			return ruleVerificationList;
		}
		for(Position p : positionList){
			if(StringUtils.isBlank(p.getWorkcode())){
				continue;
			}
			Position serialNo1 = this.getPositionForSameLinkLine(positionList, p, 1);
			
			if(serialNo1 !=null && SkillUtil.isTeacher(serialNo1,serialNo1.getCrew())
					&& p.getSerialNo() != null && p.getSerialNo() == 2){
				CrewFlightExperience crewFlightExperience = p.getCrew().getCrewFlightExperience();
				int rank = crewFlightExperience.getSeatDurationAllRank();	
				int ruleScore =  (100-rank)*assignmentRule.getRuleScore()/100;
				String remark = "本航线1号位教员，优先安排经历较小的副驾"
						+"副驾经历时间："+crewFlightExperience.getSeatDurationAll()
						+"，转换百分制名次为："+rank;
				RuleVerification r = ruleHardCheckService.getRuleVerification(p, true, remark, ruleCode, ruleScore);
				ruleVerificationList.add(r);
			}
		}
		return ruleVerificationList;
	}
	//F_S0015	机长位置：优先安排新机长
	private List<RuleVerification> F_S0015(CrewLinkLineRoster crewLinkLineRoster) {
		List<RuleVerification>  ruleVerificationList = new ArrayList<>();
		List<Position> positionList = crewLinkLineRoster.getPositionList();
		String ruleCode = Thread.currentThread().getStackTrace()[1].getMethodName();
		AssignmentRule assignmentRule = ruleHardCheckService.getAssignmentRule(crewLinkLineRoster, ruleCode);
		if(!assignmentRuleService.assignmentRuleAbled(assignmentRule)){
			return ruleVerificationList;
		}
		for(Position p : positionList){
			if(StringUtils.isBlank(p.getWorkcode())){
				continue;
			}			
			if(p.getSerialNo() != null && p.getSerialNo() == 1
					&& SkillUtil.isNewCaption(p,p.getCrew())){
				
				int ruleScore = assignmentRule.getRuleScore();
				String remark = "新机长，优先安排";
				RuleVerification r = ruleHardCheckService.getRuleVerification(p, true, remark, ruleCode, ruleScore);
				ruleVerificationList.add(r);
			}
		}
		return ruleVerificationList;
	}
	
	//F_S0017	管理干部优先安排非过夜航班
	private List<RuleVerification> F_S0017(CrewLinkLineRoster crewLinkLineRoster) {
		List<RuleVerification>  ruleVerificationList = new ArrayList<>();
		List<Position> positionList = crewLinkLineRoster.getPositionList();
		String ruleCode = Thread.currentThread().getStackTrace()[1].getMethodName();
		AssignmentRule assignmentRule = ruleHardCheckService.getAssignmentRule(crewLinkLineRoster, ruleCode);
		if(!assignmentRuleService.assignmentRuleAbled(assignmentRule)){
			return ruleVerificationList;
		}
		for(Position p : positionList){
			if(StringUtils.isBlank(p.getWorkcode())){
				continue;
			}			
			if(p.getCrew().getManagement() == true
					&& p.getLinkLine().getNightFly() == false){
				
				int ruleScore = assignmentRule.getRuleScore();
				String remark = "管理干部，并且是非过夜航线，优先安排";
				RuleVerification r = ruleHardCheckService.getRuleVerification(p, true, remark, ruleCode, ruleScore);
				ruleVerificationList.add(r);
			}
		}
		return ruleVerificationList;
	}
	
	//F_S0018	新机长，副驾位置优先排经历时间较长的副驾。
	private List<RuleVerification> F_S0018(CrewLinkLineRoster crewLinkLineRoster) {
		List<RuleVerification>  ruleVerificationList = new ArrayList<>();
		List<Position> positionList = crewLinkLineRoster.getPositionList();
		String ruleCode = Thread.currentThread().getStackTrace()[1].getMethodName();
		AssignmentRule assignmentRule = ruleHardCheckService.getAssignmentRule(crewLinkLineRoster, ruleCode);
		if(!assignmentRuleService.assignmentRuleAbled(assignmentRule)){
			return ruleVerificationList;
		}
		for(Position p : positionList){
			if(StringUtils.isBlank(p.getWorkcode())){
				continue;
			}
			
			//查询机长
			Position position1 = this.getPositionForSameLinkLine(positionList, p, 1);
			if(p.getSerialNo() !=null && p.getSerialNo() ==2 && position1!=null && SkillUtil.isNewCaption(position1,position1.getCrew())){				
				CrewFlightExperience crewFlightExperience = p.getCrew().getCrewFlightExperience();
				int rank = crewFlightExperience.getSeatDurationAllRank();	
				int ruleScore = rank*assignmentRule.getRuleScore()/100;
				String remark = "本航线1号为新机长，优先安排经历较长的副驾"
						+"副驾经历时间："+crewFlightExperience.getSeatDurationAll()
						+"，转换百分制名次为："+rank;
				
				RuleVerification r = ruleHardCheckService.getRuleVerification(p, true, remark, ruleCode, ruleScore);
				ruleVerificationList.add(r);
			}
		}
		return ruleVerificationList;
	}
	
	

	//F_S0019	近一周有病假记录的驾驶员优先排非夜航非过夜班
	private List<RuleVerification> F_S0019(CrewLinkLineRoster crewLinkLineRoster) {
		List<RuleVerification>  ruleVerificationList = new ArrayList<>();
		List<Position> positionList = crewLinkLineRoster.getPositionList();
		String ruleCode = Thread.currentThread().getStackTrace()[1].getMethodName();
		AssignmentRule assignmentRule = ruleHardCheckService.getAssignmentRule(crewLinkLineRoster, ruleCode);
		if(!assignmentRuleService.assignmentRuleAbled(assignmentRule)){
			return ruleVerificationList;
		}
		for(Position p : positionList){
			if(StringUtils.isBlank(p.getWorkcode())){
				continue;
			}
			CrewFlightExperience crewFlightExperience = p.getCrew().getCrewFlightExperience();
			if(crewFlightExperience.getLastSickLeave() <=7 && p.getLinkLine().getNightPass() == false){				
				int ruleScore = assignmentRule.getRuleScore();
				String remark = "近一周有病假，并且本次航班为非过夜航班，优先安排";
				RuleVerification r = ruleHardCheckService.getRuleVerification(p, true, remark, ruleCode, ruleScore);
				ruleVerificationList.add(r);
			}
		}
		return ruleVerificationList;
	}
	
	
	//F_S0020	外籍优先安排
	private List<RuleVerification> F_S0020(CrewLinkLineRoster crewLinkLineRoster) {
		List<RuleVerification>  ruleVerificationList = new ArrayList<>();
		List<Position> positionList = crewLinkLineRoster.getPositionList();
		String ruleCode = Thread.currentThread().getStackTrace()[1].getMethodName();
		AssignmentRule assignmentRule = ruleHardCheckService.getAssignmentRule(crewLinkLineRoster, ruleCode);
		if(!assignmentRuleService.assignmentRuleAbled(assignmentRule)){
			return ruleVerificationList;
		}
		for(Position p : positionList){
			if(StringUtils.isBlank(p.getWorkcode())){
				continue;
			}
		
			if(p.getCrew().getNationCn() == false){				
				int ruleScore = assignmentRule.getRuleScore();
				String remark = "外籍优先安排";
				RuleVerification r = ruleHardCheckService.getRuleVerification(p, true, remark, ruleCode, ruleScore);
				ruleVerificationList.add(r);
			}
		}
		return ruleVerificationList;
	}
	
	/**
	 * 从空间中获取本号位同一个连线的对应的序号的号位
	 * @param positionList
	 * @param position
	 * @param serialNo
	 * @return
	 */
	private Position getPositionForSameLinkLine(List<Position> positionList,Position position,int serialNo){
		List<Position> positionListForSameLinkLine = this.getPositionListForSameLinkLine(positionList, position);
		for(Position p : positionListForSameLinkLine){
			if(p.getSerialNo() !=null && p.getSerialNo() == serialNo){
				return p;
			}
		}
		return null;
	}
	
	/**
	 * 获取一个号位同一个连线的所有号位
	 * @param positionList
	 * @param position
	 * @return
	 */
	private List<Position> getPositionListForSameLinkLine(List<Position> positionList,Position position){
		List<Position> positionListForSameLinkLine =  new ArrayList<>();
		for(Position pOther : positionList){
			if(StringUtils.equals(pOther.getCrewLinkLineKey(), position.getCrewLinkLineKey())){
				positionListForSameLinkLine.add(pOther);
			}
		}
		return positionListForSameLinkLine;
	}
}
